from utils import *


def process_abstract(pmid):
    gold = readData(pmid + '.a2')
    triggers = [i for i in gold if i[0].startswith('T')]
    events = [i for i in gold if i[0].startswith('E')]
    if events == []:
        return
        #return (0, 0, 0, 0, 0, 0, 0, 0, 0,)
    context = [i for i in gold if i[0].startswith('M')]
    
    forest = abstrac2forest(pmid, True, True)
    
    molec_events = build_events(events, context, forest)
    
    #(total, has_cue, com_tr, com_pr, com_both, eI, eII, eIII) = check_command(molec_events, "S")
    
    if ML:
        feature_list = extract_features(molec_events, classs = CLASS, X = ["S", "VP", "NP", "JJ", "PP"])    #["S", "VP", "NP", "JJ", "PP"]



        for feature in feature_list:
            feat.write(feature.__str__() + ';' + str(pmid) + '\n') # + str(pmid)
            if TRAINING:
                feat.write(feature.__str__() + '\n') # To boost SVM learning :p

    "End of the machine learning block"
    
    #return (total, has_cue, com_tr, com_pr, com_both, len(events), eI, eII, eIII,)


if __name__ == "__main__":
    pmids = []
    for f in os.listdir(directory):
        if not f.endswith('.a1'): continue
        pmids.append(os.path.join(directory, f[:-3]))


    if ML:
        if not os.path.exists('features'):
            os.mkdir('features')
        if TRAINING:
            feat = open('features/training' + str(CLASS) + '.features', 'w')
        else:
            feat = open('features/development' + str(CLASS) + '.features', 'w')

    total =  has_cue = com_tr = com_pr = com_any = lenall = eI = eII = eIII = 0
    
    #tagtypes = set()

    for pmid in pmids:
        #print pmid
        process_abstract(pmid)

        #x = process_abstract(pmid)
        '''
        total += x[0]
        has_cue += x[1]
        com_tr += x[2]
        com_pr += x[3]
        com_any += x[4]
        lenall += x[5]
        eI += x[6]
        eII += x[7]
        eIII += x[8]
        '''

    #tags = open ('FEATURES/tagtypes.py', 'w')
    #tags.write(repr(tagtypes))
    #tags.close()

    #print (total, has_cue, com_tr, com_pr, com_any, lenall, eI, eII, eIII) 
    
    if ML:
        feat.close()
        feat2binary()
        
        
    #process_abstract(pmid)
